package cn.iocoder.yudao.module.wms.service.inventoryrecord;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.wms.controller.admin.inventoryrecord.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.inventoryrecord.InventoryRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wms.dal.mysql.inventoryrecord.InventoryRecordMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.*;

/**
 * 库存记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class InventoryRecordServiceImpl extends ServiceImpl<InventoryRecordMapper, InventoryRecordDO> implements InventoryRecordService {

    @Resource
    private InventoryRecordMapper inventoryRecordMapper;

    @Override
    public String createInventoryRecord(InventoryRecordSaveReqVO createReqVO) {
        // 插入
        InventoryRecordDO inventoryRecord = BeanUtils.toBean(createReqVO, InventoryRecordDO.class);
        String now = LocalDateTimeUtil.formatNormal(LocalDateTime.now());
        inventoryRecord.setId(UUID.randomUUID().toString())
                .setCreateAt(now)
                .setUpdateAt(now);
        inventoryRecordMapper.insert(inventoryRecord);
        // 返回
        return inventoryRecord.getId();
    }

    @Override
    public void updateInventoryRecord(InventoryRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateInventoryRecordExists(updateReqVO.getId());
        // 更新
        InventoryRecordDO updateObj = BeanUtils.toBean(updateReqVO, InventoryRecordDO.class);
        updateObj.setUpdateAt(LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
        inventoryRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteInventoryRecord(String id) {
        // 校验存在
        validateInventoryRecordExists(id);
        // 删除
        inventoryRecordMapper.deleteById(id);
    }

    private void validateInventoryRecordExists(String id) {
        if (inventoryRecordMapper.selectById(id) == null) {
            throw exception(INVENTORY_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public InventoryRecordDO getInventoryRecord(String id) {
        return inventoryRecordMapper.selectById(id);
    }

    @Override
    public PageResult<InventoryRecordDO> getInventoryRecordPage(InventoryRecordPageReqVO pageReqVO) {
        return inventoryRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InventoryRecordAnalyzeVO> analyze(Integer type) {
        List<InventoryRecordAnalyzeVO> result = new ArrayList<>();
        List<InventoryRecordDO> all = this.list(
                Wrappers.lambdaQuery(InventoryRecordDO.class)
                        .select(InventoryRecordDO::getId, InventoryRecordDO::getName, InventoryRecordDO::getCount)
                        .eq(InventoryRecordDO::getType, type)
        );
        Map<String, Integer> map = all.stream().collect(
                Collectors.groupingBy(InventoryRecordDO::getName, Collectors.summingInt(InventoryRecordDO::getCount))
        );
        for (String key : map.keySet()) {
            result.add(new InventoryRecordAnalyzeVO(key, map.get(key)));
        }
        return result;
    }
}